﻿
Public Class UIValidationError

#Region " Declarations "

    Private _strDataItemName As String = String.Empty
    Private _strErrorMessage As String = String.Empty
    Private _strPropertyName As String = String.Empty

#End Region

#Region " Properties "

    Public ReadOnly Property DataItemName() As String
        Get
            Return _strDataItemName
        End Get
    End Property

    Public ReadOnly Property ErrorMessage() As String
        Get
            Return _strErrorMessage
        End Get
    End Property

    Public ReadOnly Property Key() As String
        Get
            Return String.Format("{0}:{1}", _strDataItemName, _strPropertyName)
        End Get
    End Property

    Public ReadOnly Property PropertyName() As String
        Get
            Return _strPropertyName
        End Get
    End Property

#End Region

#Region " Constructors "

    Public Sub New(ByVal strDataItemName As String, ByVal strPropertyName As String, ByVal strErrorMessage As String)
        _strDataItemName = strDataItemName
        _strPropertyName = strPropertyName
        _strErrorMessage = strErrorMessage
    End Sub

#End Region

#Region " Methods "

    Public Function ToErrorMessage() As String
        Return String.Concat(CamelCaseString.GetWords(Me.PropertyName), " ", Me.ErrorMessage)
    End Function

    Public Function ToFriendlyErrorMessage() As String

        Dim strErrorMessage As String = Nothing

        If Me.ErrorMessage.Contains("not recognized as a valid DateTime") Then
            strErrorMessage = "date is not a valid format."

        ElseIf Me.ErrorMessage.Contains("not in a correct format.") Then
            strErrorMessage = "entered value is not the correct data type."

        Else
            'TODO add more tests here
            strErrorMessage = Me.ErrorMessage
        End If

        Dim strPropertyName As String = Nothing

        If Me.PropertyName.Contains(".") Then
            strPropertyName = CamelCaseString.GetWords(Me.PropertyName.Substring(Me.PropertyName.LastIndexOf(".") + 1))

        Else
            strPropertyName = CamelCaseString.GetWords(Me.PropertyName)
        End If

        Return String.Concat(strPropertyName, " ", strErrorMessage)
    End Function

    Public Overrides Function ToString() As String
        Return String.Format("DataItem: {0}, PropertyName: {1}, Error: {2}", _strDataItemName, _strPropertyName, _strErrorMessage)
    End Function

#End Region

End Class

